package com.session01;

import java.time.LocalDateTime;
import java.util.InputMismatchException;
import java.util.Scanner;

public class ATM {

    public static final int MAX_CASH = 200000;
    private int cash = 100000;       //银行余额
    private UserInfo[] allUsers;
    private UserInfo theUser;       //  当前登录对象
    private UserInfo otherUser;     //  转账的对象
    Scanner input = new Scanner(System.in);

    public ATM() {
        this.allUsers = new UserInfo[3];
        allUsers[0] = new UserInfo("张三", "123456", 13000);
        allUsers[1] = new UserInfo("李四", "123456", 14000);
        allUsers[2] = new UserInfo("王五", "123456", 15000);
    }

    public void run() {
        this.welcome();
        boolean flag = this.login();
        if (flag) {
            //String answer = "y";
            //answer.equalsIgnoreCase("y")
            while (true) {
                String choice = this.chooseMenu();
                switch (choice) {
                    case "1":
                        this.query();
                        break;
                    case "2":
                        this.getMoney();
                        break;
                    case "3":
                        this.saveMoney();
                        break;
                    case "4":
                        this.trans();
                        break;
                    case "5":
                        this.modPwd();
                        break;
                    case "6":
                        this.exit();
                        break;
                    default:
                        System.out.println("没有该选项，请重新选择");
                }
/*                System.out.println("请问你是否继续后续操作'y'，否则将直接退出");
                answer = input.next();*/
            }
        } else {
            System.out.println("三次机会用完，你的卡被吞");
            System.out.println("请在三日内带有效证件到柜台处理");
        }
    }

    //欢迎界面
    private void welcome() {
        System.out.println("欢迎使用ICBC银行ATM");
    }

    // 登陆
    private boolean login() {
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入用户名：");
            String inputName = input.next();
            System.out.println("请输入密码：");
            String inputPwd = input.next();

            for (UserInfo user : allUsers) {
                if (user.getUsername().equals(inputName) && user.getPassword().equals(inputPwd)) {
                    this.theUser = user;
                    return true;
                }
            }
            System.out.println("用户名/密码有误，还有" + (2 - i) + "次机会");
        }
        return false;
    }

    //菜单选择
    private String chooseMenu() {
        System.out.println("请选择你要执行的操作：");
        System.out.println("1、查询余额" + "\n" + "2、取款" + "\n" + "3、存款" + "\n" + "4、转账" + "\n" + "5、修改密码" + "\n" + "6、退出");
        String choice = input.next();
        return choice;
    }

    //查询
    private void query() {
        System.out.println("你当前的余额是：" + this.theUser.getAccount() + "元");
    }

    //取钱
    private void getMoney() {
        //1、输入用户的取款金额
        //2、校验金额的有效性
        //负数\百元\单笔不能超过20000\不能多于余额\不能多于现金
        //3、修改用户余额值
        //4、修改ATM现金值
        //5、相应的通知信息
        System.out.println("请输入你要取出的金额：");
        String a = "([1-9][0-9]?|1[0-9]{2}|200)00";
        String takeMoney = input.next();
        if (takeMoney.matches(a)) {
            int money = Integer.parseInt(takeMoney);
            this.cash -= money;
            this.theUser.setAccount(this.theUser.getAccount() - money);
            System.out.println("取款业务实现");
        } else {
            System.out.println("本机只接受2万以内的百元整钞。");
        }
    }

    //存钱
    private void saveMoney() {
        System.out.println("请输入你要存入的金额：");
        String a = "([1-9][0-9]?|1[0-9]{2}|200)00";
        String inputMoney = input.next();
        if (inputMoney.matches(a)) {
            int money = Integer.parseInt(inputMoney);
            this.cash += money;
            this.theUser.setAccount(this.theUser.getAccount() + money);
            System.out.println("存款业务实现");
        } else {
            System.out.println("本机只接受2万以内的百元整钞。");
        }
    }

    //转账
    private void trans() {
        System.out.println("请输入你想要转账的人的名字：");
        String name = input.next();
        for (UserInfo uesr : allUsers) {
            if (uesr.getUsername().equals(name)) {
                otherUser = uesr;
            }
        }
        System.out.println("请输入你想要转账的金额：");
        String a = "((([0-9]|1[0-9]{4}|[0-9]{2}([0-9]?){2}).[0-9][0-9])|20000)";
        String takeMoney = input.next();
        if (takeMoney.matches(a)) {
            double money = Double.parseDouble(takeMoney);
            this.theUser.setAccount(this.theUser.getAccount() - money);
            this.otherUser.setAccount(this.otherUser.getAccount() + money);
            System.out.println("转账业务实现");
        } else {
            System.out.println("本机只接受2万以内的百元整钞。");
        }
    }

    //修改密码
    private void modPwd() {
        System.out.println("请输入旧密码：");
        String str1 = input.next();
        if (theUser.getPassword().equals(str1)) {
            System.out.println("请输入新密码：");
            String a = "([0-9]{6})";
            String str2 = input.next();
            if (str2.matches(a)) {
                System.out.println("请再次确认密码：");
                String str3 = input.next();
                if (str2.equals(str3)) {
                    System.out.println("密码修改成功");
                } else {
                    System.out.println("密码修改失败");
                }
            } else {
                System.out.println("密码只能由6位数字组成，请重新输入新密码");
            }
        } else {
            System.out.println("密码错误");
        }
    }

    //退出
    private void exit() {
        System.exit(0);
    }
}
